class Solution:
    def criticalConnections(self, n: int, connections: list) -> list:
        self.time = 0
        idx = {}
        low = {}
        res = []
        adj = [[] for _ in range(10**5)]

        def tarjan(u, pre=None):
            idx[u] = self.time
            low[u] = self.time
            self.time += 1
            for v in adj[u]:
                if v == pre:
                    continue
                if v not in idx:
                    tarjan(v, u)
                low[u] = min(low[u], low[v])
            if pre is not None and low[u] == idx[u]:
                res.append([pre, u])

        for connection in connections:
            connection.sort()
            adj[connection[0]] += [connection[1]]
            adj[connection[1]] += [connection[0]]

        tarjan(0)

        return res
